package cn.lili.modules.supplier.entity.dto;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.extend.ExtendClass;
import cn.lili.modules.goods.entity.dto.GoodsImportDTO;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.dto.WholesaleDTO;
import cn.lili.modules.goods.entity.enums.PurchaseRuleEnum;
import cn.lili.modules.goods.entity.enums.SalesModeEnum;
import cn.lili.modules.goods.entity.enums.SupplierEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品编辑DTO
 *
 * @author pikachu
 * @since 2020-02-24 19:27:20
 */
@EqualsAndHashCode(callSuper = true)
@ExtendClass(GoodsOperationDTO.class)
@Data
@ToString
@NoArgsConstructor
public class SupplierGoodsOperationDTO extends GoodsOperationDTO {

    private static final long serialVersionUID = 4910026621192631963L;
//    @ApiModelProperty(value = "税率 例如：百分之18.5，则值为 18.5")
//    private Double taxRate;
//
//    @ApiModelProperty(value = "含税协议价")
//    private Double thirdPrice;
//
//    @ApiModelProperty(value = "未税协议价")
//    private Double nakedPrice;


    @ApiModelProperty(value = "支持代发")
    private Boolean supportProxy = false;

    @ApiModelProperty(value = "支持采购")
    private Boolean supportPurchase = false;

    @ApiModelProperty(value = "采购规则")
    private PurchaseRuleEnum purchaseRule;

    /**
     * 批发采购规则
     */
    @ApiModelProperty(value = "批发采购规则")
    private List<WholesaleDTO> wholesaleList;
    /**
     * @see SupplierEnum
     */
    @ApiModelProperty(value = "供应商类型")
    private String supplierEnum = SupplierEnum.CUSTOM.name();


    @ApiModelProperty(value = "是否为商品模版")
    private Boolean goodsTemplateFlag = false;

    /**
     * 参数自检
     */
    public void paramsCheck() {
        //如果不支持代发，也不支持采购，抛出异常
        if (Boolean.FALSE.equals(supportProxy) && Boolean.FALSE.equals(supportPurchase)) {
            throw new ServiceException(ResultCode.SUPPLIER_GOODS_MUST_SUPPORT);
        }
        //如果支持采购，采购规则判定
        if (Boolean.TRUE.equals(supportPurchase)) {
            //采购规则不能为空
            if (purchaseRule == null) {
                throw new ServiceException(ResultCode.PURCHASE_RULE_CANT_BE_NULL);
            }
            //商品级别采购，却没有设置批发规则，抛出异常
            if (purchaseRule == PurchaseRuleEnum.SPU && (wholesaleList == null || wholesaleList.isEmpty())) {
                throw new ServiceException(ResultCode.SPU_PURCHASE_NEED_RULE);
            }
            if (wholesaleList.size() > 3) {
                throw new ServiceException(ResultCode.PURCHASE_RULE_MAX_THREE);
            }
        }


        //采购规则校验
        if (wholesaleList != null) {
            Map<Integer, List<WholesaleDTO>> map = wholesaleList.stream().collect(Collectors.groupingBy(WholesaleDTO::getNum));
            if (map.size() < wholesaleList.size()) {
                throw new ServiceException(ResultCode.PURCHASE_RULE_NUM_DUPLICATE);
            }

            //排序
            wholesaleList.sort(Comparator.comparingInt(WholesaleDTO::getNum));
            //阶梯价格校验
            for (int i = 0; i < wholesaleList.size() - 1; i++) {
                if (wholesaleList.get(i).getPrice() < wholesaleList.get(i + 1).getPrice()) {
                    throw new ServiceException(ResultCode.PURCHASE_RULE_PRICE_ERROR);
                }
            }
        }
    }

    public SupplierGoodsOperationDTO(GoodsImportDTO goodsImportDTO) {
        this.setPrice(goodsImportDTO.getPrice());
        this.setGoodsName(goodsImportDTO.getGoodsName());
        this.setIntro(goodsImportDTO.getIntro());
        this.setMobileIntro(goodsImportDTO.getIntro());
        this.setQuantity(goodsImportDTO.getQuantity());
        this.setGoodsGalleryList(goodsImportDTO.getGoodsGalleryList());
        this.setTemplateId(goodsImportDTO.getTemplate());
        this.setSellingPoint(goodsImportDTO.getSellingPoint());
        this.setSalesModel(Boolean.TRUE.equals(goodsImportDTO.getSupportPurchase()) ? SalesModeEnum.PURCHASING.name() : SalesModeEnum.RETAIL.name());
        this.setGoodsUnit(goodsImportDTO.getGoodsUnit());
        this.setGoodsType(goodsImportDTO.getGoodsType());
        this.setRelease(goodsImportDTO.getRelease());
        this.setRecommend(false);
        this.setSupportProxy(goodsImportDTO.getSupportProxy());
        this.setSupportPurchase(goodsImportDTO.getSupportPurchase());
        this.setWholesaleList(goodsImportDTO.getWholesaleList());
        this.setPurchaseRule(goodsImportDTO.getPurchaseRule());
    }

}

